#ifndef __COLOR_H___
#define __COLOR_H___

//#include <SDL.h>
#include <string.h>
#include "Logger.h"

using namespace std;

class Color {

	
private:
	int* color;

	string colorToString(const int& nro){
		std::stringstream sX;
		sX <<hex<<nro ;
		string* salida;
		salida = new string(); 
		if(sX.str().length() == 1){
			
			salida->append("0");
		}
		salida->append(sX.str());
		return (*salida);
	}

	int formarColor(const int & r, const int & g, const int & b) { 
		return (r << 16) + (g << 8) + b; 
	} 

	int getR(const int color) {
		return (color & 0xFF0000) >> 16;
	}

	int getG(const int color) {
		return (color & 0xFF00) >> 8;
	}

	int getB(const int color) {
		return color & 0xFF;
	}

	int stringToInt(string s){
		istringstream iss(s);
		int i;
		iss>>i;
		return i;
	}
	
	// PASE DE STRING HEXA A INT
	char xtod(char c){
		if (c>='0' && c<='9') return c-'0';
		if (c>='A' && c<='F') return c-'A'+10;
		if (c>='a' && c<='f') return c-'a'+10;
		return c=0;        // not Hex digit
	}
 
	int HextoDec(const char *hex, int l){
		if (*hex==0) return(l);
		return HextoDec(hex+1, l*16+xtod(*hex)); // hex+1?
	}
	// FIN - PASE DE STRING HEXA A INT
	
public:

	static const int ROJO = 0xFF0000;
	static const int VERDE = 0x00FF00;
	static const int AZUL = 0x0000FF;
	static const int BLANCO = 0xFFFFFF;
	static const int CIAN = 0x00FFFF;
	static const int MAGENTA = 0xFF00FF;
	static const int AMARILLO = 0xFFFF00;
	static const int NEGRO = 0x000000;
	static const int GRIS = 0x808080;
	static const int ROSA = 0xFF8080;
	static const int VIOLETA = 0x800080;
	static const int CELESTE = 0x80FFFF;
	static const int MARRON = 0x800000;
	static const int NARANJA = 0xFF8000;
	static const int LAVANDA = 0x8000FF;
	static const int TURQUESA = 0x00FF80;
	static const int OROVIEJO = 0xBBBB20;

	Color(const string& alpha){
		// TODO - Validar el formato #rrggbb
		string* tmp = new string(alpha);
		// Quito el numeral
		tmp->replace(0,1,"");
		this->color = new int(HextoDec(tmp->c_str(),6));
	}

	Color(const int& R,const int& G,const int& B){
		this->color = new int(this->formarColor(R,G,B));
	}

	Color(const Uint32& RGB){
		this->color = new int(RGB);
	}

	bool estaProximo(Color * otroColor, int distancia){
		//int distancia = 11;
		int deltaR, deltaG, deltaB;

		deltaR = abs( this->getR() - otroColor->getR() );
		deltaG = abs( this->getG() - otroColor->getG() );
		deltaB = abs( this->getB() - otroColor->getB() );
		bool buenaDistancia = (deltaR + deltaG + deltaB) <= distancia;
		return buenaDistancia ;
	
	}

	string getAlpha	(){
		std::stringstream ss;
		ss << "#" << colorToString(this->getR()) << colorToString(this->getG()) << colorToString(this->getB());
		return ss.str();
	}

	int getR(){
		return this->getR(getColor());
	}
	
	int getG(){
		return this->getG(getColor());
	}

	int getB(){
		return this->getB(getColor());
	}

	int getColor(){
		if(!this->color) return 0;
		return (*this->color);
	}
};

#endif